<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线路标识" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择线路标识" clearable>
          <el-option
              v-for="dict in line_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="光纤通道编号" prop="fiberNum">
        <el-select v-model="queryParams.fiberNum" placeholder="请选择光纤通道编号" clearable>
          <el-option
              v-for="dict in fiber_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预警分段区域编号" prop="sectionNo">
        <el-input v-model="form.warnSectionNo" placeholder="请输入分段区域的编号" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['warn:warnRelative:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['warn:warnRelative:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['warn:warnRelative:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['warn:warnRelative:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warnRelativeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="线路标识" align="center" prop="lineId">
        <template #default="scope">
          <dict-tag :options="line_info" :value="scope.row.lineId"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum">
        <template #default="scope">
          <dict-tag :options="fiber_info" :value="scope.row.fiberNum"/>
        </template>
      </el-table-column>
      <el-table-column label="分段区域的编号" align="center" prop="warnSectionNo" />
      <el-table-column label="预警编号" align="center" prop="warnNo" />
      <el-table-column label="预警名称" align="center" prop="warnName" />
      <el-table-column label="一级预警速率阈值" align="center" prop="warnLevel1" />
      <el-table-column label="二级预警速率阈值" align="center" prop="warnLevel2" />
      <el-table-column label="三级预警速率阈值" align="center" prop="warnLevel3" />
      <el-table-column label="速率方向" align="center" prop="rateType">
        <template #default="scope">
          <dict-tag :options="value_direction" :value="scope.row.rateType"/>
        </template>
      </el-table-column>
      <el-table-column label="速率监控间隔时间(分)" align="center" prop="rateTime" />
      <el-table-column label="基线数量" align="center" prop="baseLines" />
      <el-table-column label="基线时间范围(分)" align="center" prop="baseLineTime" />
      <el-table-column label="最小发生次数" align="center" prop="occurrence" />
      <el-table-column label="最大范围" align="center" prop="tolerance" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['warn:warnRelative:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['warn:warnRelative:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改预警段的相对预警对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="warnRelativeRef" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="线路标识" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路标识">
            <el-option
                v-for="dict in line_info"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤通道编号" prop="fiberNum">
          <el-select v-model="form.fiberNum" placeholder="请选择光纤通道编号">
            <el-option
                v-for="dict in fiber_info"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分段区域的编号" prop="warnSectionNo">
          <el-input v-model="form.warnSectionNo" placeholder="请输入分段区域的编号" />
        </el-form-item>
        <el-form-item label="预警编号" prop="warnNo">
          <el-input v-model="form.warnNo" placeholder="请输入预警编号" />
        </el-form-item>
        <el-form-item label="预警名称" prop="warnName">
          <el-input v-model="form.warnName" placeholder="请输入预警名称" />
        </el-form-item>
        <el-form-item label="一级预警速率阈值" prop="warnLevel1">
          <el-input v-model="form.warnLevel1" placeholder="请输入一级预警速率阈值" />
        </el-form-item>
        <el-form-item label="二级预警速率阈值" prop="warnLevel2">
          <el-input v-model="form.warnLevel2" placeholder="请输入二级预警速率阈值" />
        </el-form-item>
        <el-form-item label="三级预警速率阈值" prop="warnLevel3">
          <el-input v-model="form.warnLevel3" placeholder="请输入三级预警速率阈值" />
        </el-form-item>
        <el-form-item label="速率方向" prop="rateType">
          <el-select v-model="form.rateType" placeholder="请选择速率方向">
            <el-option
                v-for="dict in value_direction"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="速率监控间隔时间(min)" prop="rateTime">
          <el-input v-model="form.rateTime" placeholder="请输入速率监控间隔时间(min)" />
        </el-form-item>
        <el-form-item label="基线数量" prop="baseLines">
          <el-input v-model="form.baseLines" placeholder="请输入基线数量" />
        </el-form-item>
        <el-form-item label="基线时间范围" prop="baseLineTime">
          <el-input v-model="form.baseLineTime" placeholder="请输入基线时间范围" />
        </el-form-item>
        <el-form-item label="最小发生次数" prop="occurrence">
          <el-input v-model="form.occurrence" placeholder="请输入最小发生次数" />
        </el-form-item>
        <el-form-item label="最大范围" prop="tolerance">
          <el-input v-model="form.tolerance" placeholder="请输入最大范围" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WarnRelative">
import { listWarnRelative, getWarnRelative, delWarnRelative, addWarnRelative, updateWarnRelative } from "@/api/warn/warnSectionRelative";

const { proxy } = getCurrentInstance();
const { value_direction, line_info, fiber_info } = proxy.useDict('value_direction', 'line_info', 'fiber_info');

const warnRelativeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const warnSectionInfo = ref(undefined)

const props = defineProps({
  warnSectionInfo: {
    type: Object,
    required: true,
    default: {
      id: -1,
      fiberNum: undefined,
      sectionNo: undefined
    }
  }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lineId: null,
    fiberNum: null,
  },
  rules: {
    lineId: [
      { required: true, message: "线路标识不能为空", trigger: "change" }
    ],
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "change" }
    ],
    warnSectionNo: [
      { required: true, message: "分段区域的编号不能为空", trigger: "blur" }
    ],
    warnNo: [
      { required: true, message: "预警编号不能为空", trigger: "blur" }
    ],
    warnName: [
      { required: true, message: "预警名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询预警段的相对预警列表 */
function getList() {
  loading.value = true;
  listWarnRelative(queryParams.value).then(response => {
    warnRelativeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    lineId: null,
    fiberNum: null,
    warnSectionNo: null,
    warnNo: null,
    warnName: null,
    warnLevel1: null,
    warnLevel2: null,
    warnLevel3: null,
    rateType: null,
    rateTime: null,
    baseLines: null,
    baseLineTime: null,
    occurrence: null,
    tolerance: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("warnRelativeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警段的相对预警";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWarnRelative(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预警段的相对预警";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warnRelativeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarnRelative(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarnRelative(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预警段的相对预警编号为"' + _ids + '"的数据项？').then(function() {
    return delWarnRelative(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('warn/warnRelative/export', {
    ...queryParams.value
  }, `warnRelative_${new Date().getTime()}.xlsx`)
}

function init(){
  // console.log(`sectionInfo / warnRelative / init -- begin`)
  if (props.warnSectionInfo && props.warnSectionInfo.id > 0) {
    warnSectionInfo.value = props.warnSectionInfo
    queryParams.value.warnSectionNo = warnSectionInfo.value.sectionNo
    queryParams.value.lineId = warnSectionInfo.value.lineId
    queryParams.value.fiberNum = warnSectionInfo.value.fiberNum
    getList()
  } else {
    proxy.$alert("异常的预警分段数据！");
  }
  // console.log(`sectionInfo / warnRelative / init -- end -- ${queryParams.value}`)
}

defineExpose({
  init
})

// getList();
</script>
